//
//  SEGSanyQRCodeView.m
//  Commom
//
//  Created by Clavis on 2019/8/29.
//

#import "SEGSanyQRCodeView.h"
#import <Commom/SEGColorManager.h>
#import <Commom/UIColor+YYAdd.h>
#import "SEGCreateQRCode.h"

@interface SEGSanyQRCodeView ()

@property (nonatomic, strong) UIImageView *qrCodeImageView;
@property (nonatomic, strong) UIView *loadFailView;
@property (nonatomic, assign, readwrite) BOOL isShowQrcode;

@end

@implementation SEGSanyQRCodeView

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.backgroundColor = kColor_W;
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = kColor_W;
    }
    return self;
}

- (void)showQRCodeWithString:(NSString *)string {
    self.backgroundColor = kColor_W;
    self.isShowQrcode = YES;
    
    UIImage *qrImage = [SEGCreateQRCode createQRForString:string size:220];
    if (string.length == 0 || !qrImage) {
        [self showLoadFailView];
        return;
    }
    
    UIView *containerView = [[UIView alloc] init];
    containerView.backgroundColor = kColor_W;
    [self addSubview:containerView];
    [containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_offset(0);
    }];
    
    self.qrCodeImageView = ({
        UIImageView *imageView = [[UIImageView alloc] init];
        [containerView addSubview:imageView];
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.clipsToBounds = YES;
        imageView.image = qrImage;
        imageView;
    });
    [self.qrCodeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(LYDistanceV(220));
        make.top.mas_equalTo(LYDistanceH(52));
        make.centerX.mas_offset(0);
    }];
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(retryButtonTap:)];
    [self.qrCodeImageView addGestureRecognizer:tapGesture];
    self.qrCodeImageView.userInteractionEnabled = YES;
    
    UILabel *tipLabel = ({
        UILabel *label = [[UILabel alloc] init];
        [containerView addSubview:label];
        label.textColor = [UIColor colorWithHexString:@"#9B9B9B"];
        label.font = [UIFont systemFontOfSize:19];
        label.text = @"请将二维码靠近扫码器";
        label;
    });
    [tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.qrCodeImageView.mas_bottom).offset(15);
        make.centerX.mas_offset(0);
    }];
 
    UILabel *bottomTip = ({
        UILabel *label = [[UILabel alloc] init];
        [containerView addSubview:label];
        label.textColor = [UIColor colorWithHexString:@"#9B9B9B"];;
        label.font = [UIFont systemFontOfSize:12];
        label.text = @"若无法开门，请点击刷新或联系物业管理处询问门禁权限";
        label;
    });
    [bottomTip mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-35);
        make.centerX.offset(0);
    }];
    
    UIImageView *tipImageView = ({
        UIImageView *imageView = [[UIImageView alloc] init];
        [containerView addSubview:imageView];
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        imageView.clipsToBounds = YES;
        imageView.image = [UIImage imageWithContentsOfFile:SEGPicName(@"sany-door-image")];
        imageView;
    });
    [tipImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(bottomTip.mas_top).offset(-40);
        make.centerX.offset(0);
        make.width.mas_equalTo(148);
        make.height.mas_equalTo(108);
    }];
}

- (void)showLoadFailView {
    self.isShowQrcode = NO;
    self.backgroundColor = kColor_W;
    if (!self.loadFailView) {
        self.loadFailView = [self createLoadFailView];
    }
    [self addSubview:self.loadFailView];
    [self.loadFailView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    [self bringSubviewToFront:self.loadFailView];
}

- (UIView *)createLoadFailView {
    self.isShowQrcode = NO;
    UIView *backView = [[UIView alloc] init];
    backView.backgroundColor = kColor_W;
    
    UIImageView *tipImageView = ({
        UIImageView *imageView = [[UIImageView alloc] init];
        [backView addSubview:imageView];
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.clipsToBounds = YES;
        imageView.image = [UIImage imageWithContentsOfFile:SEGPicName(@"load_fail")];
        imageView;
    });
    [tipImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(141);
        make.height.mas_equalTo(110);
        make.centerX.mas_offset(0);
        make.centerY.mas_offset(-28);
    }];
    
    UILabel *tipLabel = ({
        UILabel *label = [[UILabel alloc] init];
        [backView addSubview:label];
        label.textColor = [UIColor colorWithHexString:@"#CCCCCC"];
        label.font = kFont_15;
        label.text = @"加载失败，请重试";
        label;
    });
    [tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(tipImageView.mas_bottom).offset(14);
        make.centerX.mas_offset(0);
    }];
    
    UIButton *retryButton = ({
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button addTarget:self action:@selector(retryButtonTap:) forControlEvents:UIControlEventTouchUpInside];
        [backView addSubview:button];
        button;
    });
    [retryButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(tipImageView);
        make.bottom.equalTo(tipLabel.mas_bottom);
    }];
    
    return backView;
}

- (void)retryButtonTap:(UIButton *)sender {
    if (self.retryBlock) {
        self.retryBlock();
    }
}

- (void)showNoauthView {
    self.backgroundColor = kColor_W;
    UIView *backView = [[UIView alloc] init];
    backView.backgroundColor = kColor_W;
    [self addSubview:backView];
    [backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    
    UIImageView *tipImageView = ({
        UIImageView *imageView = [[UIImageView alloc] init];
        [self addSubview:imageView];
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.clipsToBounds = YES;
        imageView.image = [UIImage imageWithContentsOfFile:SEGPicName(@"sany_no_auth")];
        imageView;
    });
    [tipImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(141);
        make.height.mas_equalTo(110);
        make.centerX.mas_offset(0);
        make.centerY.mas_offset(-28);
    }];
    
    UILabel *tipLabel = ({
        UILabel *label = [[UILabel alloc] init];
        [self addSubview:label];
        label.textColor = [UIColor colorWithHexString:@"#CCCCCC"];
        label.font = kFont_15;
        label.numberOfLines = 0;
        label.textAlignment = NSTextAlignmentCenter;
        label.text = @"抱歉，您暂无二维码门禁权限，请联系管理处";
        label;
    });
    [tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(tipImageView.mas_bottom).offset(14);
        make.centerX.mas_offset(0);
    }];
}

@end
